package by.alex.dzeshko.facebooksuperclient.oauth;

import by.alex.dzeshko.facebooksuperclient.ContextHolder;
import by.alex.dzeshko.facebooksuperclient.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

public class FBOAuthHelper {

	private static final String LOG_TAG = "FBOAuthHelper";
	
	private static final String CODE = "code";

	private static final String APP_ID = "103433719827676";

	private static final String APP_SECRET = "8c0ca02e1f3cc64269b4ded403a740d3";

	public static final String ACCESS_TOKEN = "access_token";

	public static final String REDIRECT_URL = "fbconnect://success";

	public static final String USER_ID = "user_id";

	public static final String AUTH_PREFS = "auth_prefs";
	
	public static String userId;

	private static final String URL_GETTOKEN = "https://graph.facebook.com/oauth/access_token?"
			+ "client_id="		+ APP_ID
			+ "&client_secret="	+ APP_SECRET
			+ "&grant_type=client_credentials";
	private static final String LOGIN_URL = "https://www.facebook.com/dialog/oauth?client_id="	+ APP_ID
			+ "&redirect_uri="	+ REDIRECT_URL
			+ "&scope=email,user_about_me,user_status,user_religion_politics,user_birthday,user_photos,user_relationships,user_location,"
			+ "friends_about_me,friends_birthday,friends_location,friends_photos,friends_status,"
			+ "read_friendlists,read_requests,read_stream,"
			+ "publish_stream,manage_friendlists,publish_actions"
			+ "&display=touch&type=user_agent";


	public static String getTokenUrl() {
		return URL_GETTOKEN;
	}

	public static String getLoginUrl() {
		return LOGIN_URL;
	}

	public static boolean isRedirectURL(String url) {
		if (url.startsWith(REDIRECT_URL)) {
			return true;
		} else {
			return false;
		}
	}

	public static String saveCreditionals(final Context context, String url) {

		url = url.substring(url.indexOf("#"));
		String[] creds = url.split("&");
		String token = null, code = null;
		for (String i : creds) {
			if (i.contains(ACCESS_TOKEN))
				token = i.substring(i.indexOf("=") + 1);
			if (i.contains(CODE))
				code = i.substring(i.indexOf("=") + 1);
		}
		SharedPreferences prefs = context.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putString(ACCESS_TOKEN, token);
		editor.putString(CODE, code);
		editor.commit();
		return token;
	}

	public static void exit(Context context) {
		try {
			CookieSyncManager cookieSyncMngr = CookieSyncManager
					.createInstance(context);
			CookieManager cookieManager = CookieManager.getInstance();
			cookieManager.removeAllCookie();
		} catch (Exception e) {
			Log.e(LOG_TAG, e.toString());
		}
		Editor editor = context.getSharedPreferences(AUTH_PREFS,
				Context.MODE_PRIVATE).edit();
		editor.clear();
		editor.commit();
	}

	public static boolean isLogged(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE);
		return savedSession.getString(ACCESS_TOKEN, null) != null;
	}
	
	public static String getToken(){
		Context ctx = ContextHolder.getInstance().getContext();
		SharedPreferences savedSession = ctx.getSharedPreferences(AUTH_PREFS, Context.MODE_PRIVATE);
		return savedSession.getString(ACCESS_TOKEN, null);
	}
}
/*ads_management
create_event
create_note
export_stream
friends_online_presence
manage_friendlists
manage_notifications
manage_pages
offline_access
photo_upload
publish_checkins
publish_stream
read_friendlists
read_insights
read_mailbox
read_page_mailboxes
read_requests
read_stream
rsvp_event
share_item
sms
status_update
user_online_presence
video_upload
xmpp_login*/
/*
user_actions.music
user_actions.news
user_actions.video
user_activities
user_education_history
user_events
user_games_activity
user_groups
user_hometown
user_interests
user_likes
user_notes
user_questions
user_relationship_details
user_subscriptions
user_videos
user_website
user_work_history*/
/*
friends_actions.music
friends_actions.news
friends_actions.video
friends_activities
friends_education_history
friends_events
friends_games_activity
friends_groups
friends_hometown
friends_interests
friends_likes
friends_notes
friends_questions
friends_relationship_details
friends_relationships
friends_religion_politics
friends_subscriptions
friends_videos
friends_website
friends_work_history*/